package app.linksheet.feature.shizuku.ui

import androidx.activity.compose.LocalActivity
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Info
import androidx.compose.material.icons.rounded.InstallMobile
import androidx.compose.material.icons.rounded.NotStarted
import androidx.compose.material.icons.rounded.WarningAmber
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import app.linksheet.compose.page.SaneScaffoldSettingsPage
import app.linksheet.compose.preview.PreviewTheme
import app.linksheet.feature.shizuku.R
import app.linksheet.feature.shizuku.ShizukuDownload
import app.linksheet.feature.shizuku.ShizukuStatus
import app.linksheet.feature.shizuku.viewmodel.ShizukuSettingsViewModel
import fe.android.compose.icon.iconPainter
import fe.android.compose.text.StringResourceContent.Companion.textContent
import fe.android.compose.text.TextContent
import fe.android.compose.text.TextOptions
import fe.composekit.component.ContentType
import fe.composekit.component.PreviewThemeNew
import fe.composekit.component.card.AlertCard
import fe.composekit.component.card.AlertCardDefaults
import fe.composekit.layout.column.SaneLazyListScope
import fe.composekit.lifecycle.collectRefreshableAsStateWithLifecycle
import fe.composekit.preference.FakePreferences
import org.koin.androidx.compose.koinViewModel

@Composable
fun ShizukuRoute(
    onBackPressed: () -> Unit,
    viewModel: ShizukuSettingsViewModel = koinViewModel(),
) {
    val activity = LocalActivity.current

    val status by viewModel.status.collectRefreshableAsStateWithLifecycle(
        minActiveState = Lifecycle.State.RESUMED,
        initialValue = remember { viewModel.status.value },
    )

    ShizukuRouteInternal(
        status = status,
        openManager = {
            viewModel.startManager(activity)
        },
        requestPermission = viewModel::requestPermission,
        onBackPressed = onBackPressed,
        content = {
            content(
                enableShizukuPref = viewModel.enableShizuku,
                autoDisableLinkHandlingPref = viewModel.autoDisableLinkHandling
            )
        }
    )
}

@Composable
private fun ShizukuRouteInternal(
    status: ShizukuStatus,
    openManager: () -> Unit,
    requestPermission: () -> Unit,
    onBackPressed: () -> Unit,
    content: SaneLazyListScope.() -> Unit,
) {
    SaneScaffoldSettingsPage(
        headline = stringResource(id = R.string.settings_shizuku__title_shizuku),
        onBackPressed = onBackPressed
    ) {
        when {
            !status.installed -> notInstalled()
            !status.running -> notRunning(openManager = openManager)
            !status.permission -> noPermission(requestPermission = requestPermission)
            else -> content()
        }

        item {
            Spacer(modifier = Modifier.height(24.dp))
        }

        item(key = R.string.settings_shizuku__text_info) {
            InfoText(textContent = textContent(R.string.settings_shizuku__text_info))
        }
    }
}

private fun SaneLazyListScope.notInstalled() {
    item(
        key = R.string.settings_shizuku__title_not_installed,
        contentType = ContentType.SingleGroupItem
    ) {
        val uriHandler = LocalUriHandler.current
        AlertCard(
            colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.secondaryContainer),
            icon = Icons.Rounded.InstallMobile.iconPainter,
            iconOffset = AlertCardDefaults.IconOffset,
            iconContentDescription = stringResource(id = R.string.settings_shizuku__title_not_installed),
            headline = textContent(R.string.settings_shizuku__title_not_installed),
            subtitle = textContent(R.string.settings_shizuku__text_not_installed),
            onClick = {
                uriHandler.openUri(ShizukuDownload)
            }
        )
    }
}

private fun SaneLazyListScope.notRunning(openManager: () -> Unit) {
    item(
        key = R.string.settings_shizuku__title_not_running,
        contentType = ContentType.SingleGroupItem
    ) {
        AlertCard(
            colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.secondaryContainer),
            icon = Icons.Rounded.NotStarted.iconPainter,
            iconOffset = AlertCardDefaults.IconOffset,
            iconContentDescription = stringResource(id = R.string.settings_shizuku__title_not_running),
            headline = textContent(R.string.settings_shizuku__title_not_running),
            subtitle = textContent(R.string.settings_shizuku__text_not_running),
            onClick = openManager
        )
    }
}

private fun SaneLazyListScope.noPermission(requestPermission: () -> Unit) {
    item(
        key = R.string.settings_shizuku__title_missing_permission,
        contentType = ContentType.SingleGroupItem
    ) {
        AlertCard(
            colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.secondaryContainer),
            icon = Icons.Rounded.WarningAmber.iconPainter,
            iconOffset = AlertCardDefaults.IconOffset,
            iconContentDescription = stringResource(id = R.string.settings_shizuku__title_missing_permission),
            headline = textContent(R.string.settings_shizuku__title_missing_permission),
            subtitle = textContent(R.string.settings_shizuku__text_missing_permission),
            onClick = requestPermission
        )
    }
}

@Preview
@Composable
private fun ShizukuRouteInternalPreview() {
    PreviewThemeNew {
//        ShizukuRouteInternal(onBackPressed = {})
    }
}

@Preview
@Composable
private fun ShizukuRouteNotInstalledPreview() {
    ShizukuPreviewBase(status = ShizukuStatus(installed = false, permission = false, running = false))
}

@Preview
@Composable
private fun ShizukuRouteNoPermissionPreview() {
    ShizukuPreviewBase(status = ShizukuStatus(installed = true, permission = false, running = true))
}

@Preview
@Composable
private fun ShizukuRouteNotRunningPreview() {
    ShizukuPreviewBase(status = ShizukuStatus(installed = true, permission = true, running = false))
}

@Preview
@Composable
private fun ShizukuRouteContentPreview() {
    ShizukuPreviewBase(status = ShizukuStatus(installed = true, permission = true, running = true))
}

@Composable
private fun ShizukuPreviewBase(status: ShizukuStatus) {
    PreviewTheme {
        ShizukuRouteInternal(
            status = status,
            openManager = {},
            requestPermission = {},
            onBackPressed = {},
            content = {
                content(
                    enableShizukuPref = FakePreferences.boolean(true).vm,
                    autoDisableLinkHandlingPref = FakePreferences.boolean(true).vm
                )
            }
        )
    }
}

@Composable
private fun InfoText(textContent: TextContent) {
    Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
        Icon(imageVector = Icons.Outlined.Info, contentDescription = null)

        TextContent(
            textContent = textContent,
            textOptions = TextOptions(style = MaterialTheme.typography.bodySmall),
        )
    }
}


@Preview(showBackground = true)
@Composable
private fun InfoTextPreview() {

}
